package com.admin.controller;

import java.util.Date;

import com.admin.model.Sys_User;
import com.admin.util.AjaxObject;
import com.admin.util.Constants;
import com.admin.util.MD5;
import com.admin.util.Page;
import com.admin.util.UtilTools;

/**
 * 管理员列表
 * @author caohuayang
 *
 */
public class AdminController extends BaseController {
	public static String ADMIN = "/admin";
	/**
	 * 获取管理员列表
	 */
	public void index(){
		// 查询条件
		Sys_User user = getModel(Sys_User.class);
		String userName = user.get("username");
		String uname = null;
		if(UtilTools.isNotNull(userName)){
			uname = "u2.userName like '%"+ userName +"%'";
		}
		String loginName = user.get("loginname");
		String lname = null;
		if(UtilTools.isNotNull(loginName)){
			lname = "u2.loginname like '%"+ loginName +"%'";
		}
		String mobile = user.get("mobile");
		String qMobile = null;
		if(UtilTools.isNotNull(mobile)){
			qMobile = "u2.mobile like '%"+ mobile +"%'";
		}
		String email = user.get("email");
		String qEmail = null;
		if(UtilTools.isNotNull(email)){
			qEmail = "u2.email like '%"+ email +"%'";
		}
		Integer state = user.getInt("active");
		String qState = null;
		if(UtilTools.isNotNull(state)){
			qState = "u2.active = '"+ state +"'";
		}
		Page<Sys_User> page = new Page<Sys_User>(getPara("pageCurrent"),getPara("pageSize"));
		page.setOrderField(getPara("orderField"));
		page.setOrderDirection(getPara("orderDirection"));
		Sys_User.dao.listByPage(new String[]{"u2.deleted = 0",uname,lname,qEmail,qMobile,qState},page);
		setAttr("page",page);
		this.render("/jsp/admin/list.jsp");
	}
	
	// 详情
	public void detail(){
		Sys_User user = Sys_User.dao.getUser(getParaToLong("id"));
		setAttr("user",user);
		render("/jsp/admin/update.jsp");
	}
	
	// 验证账号是否已存在
	public void checkName(){
		String msg = "";
		try {
			Sys_User user = getModel(Sys_User.class);
			String name = getPara("name");
			if(!name.equals(user.get("loginname"))){
				long count = Sys_User.dao.getCount(new String[]{"loginname='"+ user.get("loginname") +"'","deleted=0"});
				if(count == 0){
					msg = "{\"ok\":\"登录名可用\"}"; 
				}else{
					msg = "{\"error\":\"登录名已存在，请更换\"}"; 
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		renderJson(msg);
	}
	
	// 保存信息
	public void save(){
		try {
			Sys_User loginUser = getSessionAttr(Constants.LOGIN_USER);
			Sys_User user = getModel(Sys_User.class);
			if (null == user.get("id")) {
				user.set("password",MD5.encode("123456"));
				user.set("date_created",new Date());
				user.set("date_last_modified",new Date());
				user.set("user_id_created",loginUser.get("id"));
				user.save();
			} else {
				user.set("date_last_modified",new Date());
				user.set("user_id_last_modified",loginUser.get("id"));
				user.update();
			}
			ajax(Status.success,ADMIN,true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage(), e);
			ajax(Status.error,null,false);
		}
	}
	
	// 密码验证
	public void validatePwd(){
		try {
			String pwd = getPara("pwd");
			Sys_User user = getSessionAttr(Constants.LOGIN_USER);
			if(user.get("password").equals(MD5.encode(pwd.trim()))){
				renderJson(true);
			}else{
				renderJson(false);			
			}
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(false);
		}
	}
	
	// 修改密码
	public void updatePwd(){
		try {
			String pwd = getPara("passWord");
			Sys_User user = getSessionAttr(Constants.LOGIN_USER);
			boolean result = Sys_User.dao.updatePwd(user.getLong("id"),pwd);
			if(result){
				ajax(Status.success,null,true);
			}else{
				ajax(Status.error,null,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajax(Status.error,null,false);
		}
	}
	
	// 删除
	public void del(){
		try {
			String[] ids = getParaValues("ids");
			boolean result = Sys_User.dao.del(ids);
			if(result){
				ajax(Status.success,null,false);
			}else{
				ajax(Status.error,null,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(ajax(Status.error,"删除失败"));
		}
	}
	
	// 重置密码
	public void resetPwd(){
		try {
			String[] ids = getParaValues("ids");
			boolean result = Sys_User.dao.resetPwd(ids);
			if(result){
				ajax(Status.success,null,false);
			}else{
				ajax(Status.error,null,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajax(Status.error,null,null);
		}
	}
	
	// 修改状态
	public void updateState(){
		try {
			String[] ids = getParaValues("ids");
			boolean result = Sys_User.dao.updateState(ids);
			if(result){
				ajax(Status.success,ADMIN,false);
			}else{
				ajax(Status.error,null,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajax(Status.error,null,null);
		}
	}
	
	// 跳转到角色选择页面
	public void update_role_view(){
		try {
			String userIds = getPara("userIds").toString();
			String userNames = Sys_User.dao.getUserName(userIds.split(","));
			setAttr("userIds",userIds);
			setAttr("userNames",userNames);
			render("/jsp/admin/updateRole.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 更新角色
	public void updateRole(){
		try {
			String[] userIds = getPara("userIds").toString().split(",");
			String roleIds = getPara("roleIds").toString();
			boolean result = Sys_User.dao.updateRole(userIds, roleIds);
			if(result){
				ajax(Status.success,ADMIN,true);
			}else{
				ajax(Status.error,null,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajax(Status.error,null,null);
		}
	}
	
	// 跳转到订单来源选择页面
	public void update_orders_source_view(){
		try {
			String userIds = getPara("userIds").toString();
			String userNames = Sys_User.dao.getUserName(userIds.split(","));
			setAttr("userIds",userIds);
			setAttr("userNames",userNames);
			render("/jsp/admin/updateOrdersSource.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 更新订单来源
	public void updateOrdersSource(){
		try {
			String[] userIds = getPara("userIds").toString().split(",");
			String sourceIds = getPara("sourceIds").toString();
			boolean result = Sys_User.dao.updateOrdersSource(userIds, sourceIds);
			if(result){
				ajax(Status.success,ADMIN,true);
			}else{
				ajax(Status.error,null,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("异常:"+e.getMessage());
			ajax(Status.error,null,null);
		}
	}
}
